/*================================================================
*   Copyright (C) 2024 baichao All rights reserved.
*
*   文件名称：main2.c
*   创 建 者：baichao
*   创建日期：2024年01月06日
*   描    述：
*
================================================================*/

#include <stdio.h>
#include <stdlib.h>

int* productExceptSelf(int* nums, int numsSize, int* returnSize) {
  int* answer = (int*)malloc(numsSize * sizeof(int));
  *returnSize = numsSize;

  // 初始化answer数组
  answer[0] = 1;
  for (int i = 1; i < numsSize; i++) {
    answer[i] = answer[i - 1] * nums[i - 1];
  }

  // 从右侧开始计算乘积
  int rightProduct = 1;
  for (int i = numsSize - 1; i >= 0; i--) {
    answer[i] *= rightProduct;
    rightProduct *= nums[i];
  }

  return answer;
}

int main() {
  int nums[] = {1, 2, 3, 4};
  int returnSize = 0;
  int* ret =
      productExceptSelf(nums, sizeof(nums) / sizeof(nums[0]), &returnSize);
  printf("[");
  for (int i = 0; i < returnSize; i++) {
    printf("%d,", ret[i]);
  }
  printf("]\n");
  return 0;
}
